package com.ttbj.controller.order.move;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.constant.GenerateNumConstants;
import com.ttbj.controller.order.DefaultOrderController;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.OriginalPayFacade;
import com.ttbj.product.bean.dto.CargoDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.OwnerDto;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.DateUtils;
import com.ttbj.util.RequestUtils;
import com.ttbj.util.TokenUtils;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/applet/move")
public class MoveOrderController {

	@Autowired
	OrderFacade orderFacade = null;
	
	@Autowired
	OriginalPayFacade  originalPayFacade = null;

  /**
   * 生成搬家订单
   * sender为搬出信息
   * reciver 为搬入信息
   * @param request
   * @param response
   * @param openId
   * @param senderName
   * @param senderPhone
   * @param senderProvince
   * @param senderCity
   * @param senderArea
   * @param senderAddr
   * @param senderLat
   * @param senderLng
   * @param reciverName
   * @param reciverPhone
   * @param reciverProvince
   * @param reciverCity
   * @param reciverArea
   * @param reciverAddr
   * @param reciverLat
   * @param reciverLng
   * @param orderNotes
   * @param discountId
   * @param prearrangeTime
   * @return
   * @throws Exception
   */
	@RequestMapping("/new.do")
	@ResponseBody
	public ViewData moveOrderNew(HttpServletRequest request ,
			HttpServletResponse response,
			@RequestParam("openId") String openId,
			@RequestParam("outName") String senderName,
			@RequestParam("outPhone") String senderPhone,
			@RequestParam("outProvince") String senderProvince,
			@RequestParam("outCity") String senderCity,
			@RequestParam("outArea") String senderArea,
			@RequestParam("outAddr") String senderAddr,
			@RequestParam("outLat") String senderLat,
			@RequestParam("outLng") String senderLng,
			@RequestParam(value="inName",required=false) String reciverName,
			@RequestParam(value="inPone",required=false) String reciverPhone,
			@RequestParam("inProvince") String reciverProvince,
			@RequestParam("inCity") String reciverCity,
			@RequestParam("inArea") String reciverArea,
			@RequestParam("inAddr") String reciverAddr,
			@RequestParam("inLat") String reciverLat,
			@RequestParam("inLng") String reciverLng,
			@RequestParam("prearrangeTime") String prearrangeTime,
			String orderNotes,
			Long  discountId
			) throws Exception{
		UserDto userDto = (UserDto)request.getAttribute("user");
		String curDttStr = DateUtils.getCurDttStr();
		if(StringUtils.isEmpty(reciverName)){
			reciverName = senderName;
		}
		if(StringUtils.isEmpty(reciverPhone)){
			  reciverPhone = senderPhone;
		}
		OwnerDto sender = new OwnerDto(TokenUtils.getNo(GenerateNumConstants.OWNER), senderName, senderPhone, senderProvince, senderCity, senderArea, senderAddr, curDttStr,openId,senderLat,senderLng);
		OwnerDto reciver = new OwnerDto(TokenUtils.getNo(GenerateNumConstants.OWNER), reciverName, reciverPhone, reciverProvince, reciverCity, reciverArea, reciverAddr,curDttStr,openId,reciverLat,reciverLng);
		OrderDto orderDto = new OrderDto(TokenUtils.getNo(GenerateNumConstants.ORDER), curDttStr,userDto.getId(),prearrangeTime,orderNotes,discountId);
		
		orderFacade.createMoveOrder(sender, reciver,openId,orderDto);
		return ViewModelUtil.buildSuccess("成功");
	}
	
	/**
	 * 用户缴纳搬家费用(用户完成缴费)
	 * 作用：1.业务员推送给搬家用户支付费用
	 * 	    2.货运订单未支付，用户完成支付
	 * @param request
	 * @param orderId
	 * @param openId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/compl/pay.do")
	@ResponseBody
	public ViewData orderPay(HttpServletRequest request, Long orderId, String openId) throws Exception {
		OrderDto orderDto = orderFacade.queryOrder(orderId).openId(openId);
		Float orderAllPay = orderDto.getOrderAllPay();
		Map<String, String> paraMap = originalPayFacade.appletPay(RequestUtils.getRequestPath(request), orderAllPay, null, openId, String.valueOf(orderDto.getOrderId()));
		return ViewModelUtil.buildSuccess("成功", paraMap);
	}
	
	
 
	

}
